<template>
  <div class="login_container">
    <!-- 登录盒子  -->
    <div class="login_box">
      <!-- 头像 -->
      <div class="avatar_box">
        <img src="../assets/heima.png" alt="" />
      </div>
      <!-- 表单 -->
      <el-form ref="loginForm" :model="loginData" label-width="0" class="login_form" :rules="rules">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input v-model="loginData.username" prefix-icon="iconfont icon-user"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input v-model="loginData.password" prefix-icon="iconfont icon-3702mima"></el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登陆</el-button>
          <el-button type="info" @click="remove">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import '@/assets/css/global.css'
export default {
  name: 'login',
  data() {
    return {
      loginData: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 2,
            max: 15,
            message: '用户名长度在 2 到 15 个字符',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 12,
            message: '密码长度在 6 到 12 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    // 登陆方法
    login() {
      // 1. 先校验
      // 1.1 校验表单是否合法; 表单对象.validate方法
      // form表单所有的输入框校验通过，validate回调形参为true否则为false
      this.$refs.loginForm.validate(async (ispass) => {
        // 1.2 如果校验通过，则获取数据发起axios请求
        if (ispass) {
          // 2. 再提交
          // const res = await this.$axios({
          //   method: "POST",
          //   url: "login",
          //   data: this.loginData,
          // });
          console.log(this.loginData)
          const { data: res } = await this.$axios.post('login', this.loginData)
          // 错误提示
          if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
          // 正确提示
          this.$message.success(res.meta.msg)
          // 登陆成功将身份验证的token保存到本地
          sessionStorage.setItem('token', res.data.token)
          // vue-hash方式跳转至首页
          this.$router.push('/home')
        }
      })
    },
    // 重置函数
    remove() {
      // 清楚输入框中的用户名和密码
      this.loginData.username = ''
      this.loginData.password = ''
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b5b6b;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
